package o;

import android.content.Intent;
import android.os.StatFs;
import android.text.TextUtils;
import com.huawei.appgallery.downloadengine.api.SessionDownloadTask;
import java.io.File;
import java.io.FilenameFilter;

import o.ewm.n;

public class dvw extends bmz {
    public static final String ˎ = (ContentManager.getInstant().getContext().getPackageName() + ".action.storageInsufficient");
    public static final String ॱ = (ContentManager.getInstant().getContext().getPackageName() + ".DownloadDiskSpacePolicy");
    private c ˊ = new c(this);
    private dwg ˏ = new dwg();

    static class b implements FilenameFilter {
        private b() {
        }

        public boolean accept(File file, String str) {
            return str.endsWith(".apk");
        }
    }

    public class c {
        private coa ˊ;
        final /* synthetic */ dvw ॱ;

        public c(dvw o_dvw) {
            this.ॱ = o_dvw;
            fhz ˋ = fho.ˏ().ˋ("PackageManager");
            if (ˋ != null) {
                this.ˊ = (coa) ˋ.ॱ(coa.class);
            }
        }

        public boolean ॱ(SessionDownloadTask sessionDownloadTask, dwd o_dwd, a aVar, boolean z) {
            Logger.ˏ("DownloadDiskSpacePolicy", "handlerSpaceNotEnough isDiskWriteException:" + z + ",task:" + sessionDownloadTask.ˊˊ());
            if (this.ॱ.ˏ.ˏ(sessionDownloadTask, aVar, z)) {
                long j = 180000;
                do {
                    synchronized (sessionDownloadTask) {
                        try {
                            sessionDownloadTask.wait(2000);
                        } catch (InterruptedException e) {
                            Logger.ˏ("DownloadDiskSpacePolicy", "sleep for PackageService stop:InterruptedException!");
                        }
                    }
                    j -= 2000;
                    if (this.ॱ.ˏ.ˏ(sessionDownloadTask, aVar, 0)) {
                        break;
                    } else if (sessionDownloadTask.ˊᐝ()) {
                        return true;
                    } else {
                        if (ˋ() > 0 && j < 0) {
                            this.ॱ.ˋ(z);
                            Logger.e("DownloadDiskSpacePolicy", "PackageService wait timeout handlerSpaceNotEnough and enough:" + aVar.ˋ() + ",downloaded size:" + o_dwd.ˊ + ",cancelTask:" + o_dwd.ˏ);
                            return false;
                        }
                    }
                } while (this.ॱ.ˏ.ˏ(sessionDownloadTask, aVar, z));
            }
            if (sessionDownloadTask.ˊᐝ()) {
                return true;
            }
            if (!aVar.ˋ()) {
                this.ॱ.ˊ.ˏ(sessionDownloadTask, o_dwd, aVar);
            }
            if (!aVar.ˋ()) {
                if (Logger.ˎ()) {
                    Logger.ˏ("DownloadDiskSpacePolicy", "try all the methods and space not enough,pause all the task");
                }
                this.ॱ.ˋ(z);
            }
            if (Logger.ˎ()) {
                Logger.ˏ("DownloadDiskSpacePolicy", "handlerSpaceNotEnough and enough:" + aVar.ˋ() + ",downloaded size:" + o_dwd.ˊ + ",cancelTask:" + o_dwd.ˏ);
            }
            return false;
        }

        private long ˋ() {
            if (this.ˊ != null) {
                return this.ˊ.ˋ();
            }
            return 0;
        }

        public void ˏ(SessionDownloadTask sessionDownloadTask, dwd o_dwd, a aVar) {
            Object obj = null;
            if (cep.ˏ()) {
                Object obj2;
                File file = new File(aVar.ॱ());
                if (file.exists()) {
                    String[] list = file.list(new b());
                    if (list != null && list.length > 0) {
                        int length = list.length;
                        int i = 0;
                        while (i < length) {
                            String str = aVar.ॱ() + list[i];
                            if (!new File(str).delete()) {
                                Logger.ˋ("DownloadDiskSpacePolicy", "new File(filePath) delete error.");
                            }
                            if (Logger.ˎ()) {
                                Logger.ˏ("DownloadDiskSpacePolicy", "delte apk when storage not enough:" + str);
                            }
                            i++;
                            int i2 = 1;
                        }
                    }
                    obj2 = obj;
                    ((cdf) cdb.ॱ(cdf.class)).ˋ();
                } else {
                    obj2 = null;
                }
                if (obj2 != null) {
                    this.ॱ.ˏ.ˏ(sessionDownloadTask, aVar, 0);
                }
                if (!aVar.ˋ()) {
                    this.ॱ.ˏ.ˋ(sessionDownloadTask, o_dwd, aVar);
                }
            }
        }
    }

    public static long ˋ(String str) {
        try {
            StatFs statFs = new StatFs(str);
            return statFs.getAvailableBlocksLong() * statFs.getBlockSizeLong();
        } catch (Throwable e) {
            Logger.ˋ("DownloadDiskSpacePolicy", "path error", e);
            return 0;
        }
    }

    public a ˏ(SessionDownloadTask sessionDownloadTask) {
        dwd o_dwd = new dwd();
        a aVar = new a();
        aVar.ˎ(false);
        cfl ˋ = epp.ˋ();
        if (!(ˋ == null || TextUtils.isEmpty(ˋ.ˎ()))) {
            aVar.ˊ(ˋ.ˎ());
            aVar.ˎ(ˋ.ॱ());
            if (this.ˏ.ˊ(sessionDownloadTask, aVar, 0, false)) {
                aVar.ˎ(true);
            } else if (this.ˊ.ॱ(sessionDownloadTask, o_dwd, aVar, false)) {
                aVar.ˎ(true);
                Logger.ˏ("DownloadDiskSpacePolicy", "user interrupt!");
                return aVar;
            }
        }
        if (!aVar.ˋ()) {
            ˏ(sessionDownloadTask, o_dwd);
        }
        Logger.ˎ("DownloadDiskSpacePolicy", "isEnough:" + aVar.ˋ() + ",availableStoragePath:" + aVar.ॱ());
        return aVar;
    }

    protected void ˋ(boolean z) {
        if (z) {
            dvt.ˏ().ˋ(6);
        } else {
            dvt.ˏ().ˋ(5);
        }
    }

    public void ˏ(SessionDownloadTask sessionDownloadTask, a aVar) {
        aVar.ˎ(ˋ(aVar.ॱ()));
        aVar.ˎ(false);
        dwd o_dwd = new dwd();
        if (!this.ˊ.ॱ(sessionDownloadTask, o_dwd, aVar, true)) {
            dvt.ˏ().ˋ(sessionDownloadTask.ʾ(), 6);
            ˏ(sessionDownloadTask, o_dwd);
        } else if (Logger.ˎ()) {
            Logger.ˏ("DownloadDiskSpacePolicy", "user interrupt!");
        }
    }

    protected void ˏ(SessionDownloadTask sessionDownloadTask, dwd o_dwd) {
        if (epv.ᐝ(ContentManager.getInstant().getContext())) {
            Intent intent = new Intent();
            intent.setAction(ॱ);
            intent.putExtra("APP_NAME", sessionDownloadTask.ʿ());
            intent.putExtra("CANCEL_PKGS", o_dwd.ˏ);
            intent.putExtra("APP_PKG", sessionDownloadTask.ʾ());
            intent.putExtra("CLEAR_SPACE", o_dwd.ˊ);
            ll.ˏ(ContentManager.getInstant().getContext()).ˋ(intent);
        } else {
            evk.ˏ(ContentManager.getInstant().getContext().getResources().getString(n.ʻˉ));
        }
        ll.ˏ(ContentManager.getInstant().getContext()).ˋ(new Intent(ˎ));
    }
}
